package com.study.talk.ui.adapter

import android.content.Context
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.recyclerview.widget.RecyclerView
import com.study.talk.R
import com.study.talk.ui.adapter.base.BaseListAdapter
import com.study.talk.databinding.GroupUsersItemBinding
import com.study.talk.data.model.GroupUserInfo
import com.study.talk.ui.app.self.SelfActivity
import com.study.talk.utils.extras.start
import com.study.talk.utils.extras.toUserDetails

class GroupUsersAdapter(val userId: Int) :
    BaseListAdapter<GroupUserInfo, GroupUsersAdapter.ViewHolder>() {
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        return ViewHolder(
            parent.context, DataBindingUtil.inflate(
                LayoutInflater.from(parent.context),
                R.layout.group_users_item,
                parent,
                false,
            )
        )
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        holder.binding.user = getItem(position)
        //点击群信息中的成员跳转到成员详情
        holder.binding.root.setOnClickListener {
            holder.context.apply {
                if (userId != getItem(position).userId) {
                    toUserDetails(getItem(position).userId)
                }else{
                    start(SelfActivity::class.java)
                }
            }
        }
    }

    class ViewHolder(val context: Context, val binding: GroupUsersItemBinding) :
        RecyclerView.ViewHolder(
            binding.root
        )
}